
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEvent();
});

function loadData() {
    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getDataRegister',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapLevelToView(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllMemberCode',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllMemberCodeToView(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllAccount',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllAccountToView(responseContent);
        }
    });
}

function bindEvent() {
    ISS.EVENT.bindEvent('#RegeditForm_member_Add1', 'click', function () {
        //clearValidate();
        if(validate()){
            sendInfo();
        }
        else {
            var htmlText = '<div class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Đăng kí tài khoản thất bại</div>\
                            </div>';
            ISS.DOM.before('.l-register__benefits', htmlText);
        }
    });

    ISS.EVENT.bindEvent('#RegeditForm_member_Modified', 'click', function () {
        toEditPage();
    });
}

function validate() {
    var result = true;
    return result;
}

function sendInfo() {
    var user = {};
    var capbac = {};
    var code = {};

    code.maTV = ISS.DOM.getVal('#AdMemberAccount_user');
    user.thongTinThanhVien = code;

    user.matKhau = ISS.DOM.getVal('#AdMemberAccount_pass');

    capbac.maCB = ISS.DOM.getVal('#AdMemberAccount_level');
    user.capBac = capbac;

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/header/AdMemberAccount',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(user),
        success: function (data) {
            var responseContent = data.responseObject;

        }
    });
}

function toEditPage() {
    var _maTV = [];
    ISS.DOM.forEach('#form-RegeditFormMember input[type="checkbox"]:checked', function(){
        _maTV.push($(this).val());
    });

    if(_maTV.length == 1) {
        var _url = '/service/admin/toMemberAccountPage/' + _maTV[0] + '.';
        ISS.WINDOW.location(_url);
    }
    else {
        alert('Bạn chưa chọn hoặc đã chọn hơn 1 dòng');
    }
}

function clearValidate() {
    ISS.DOM.removeClass('#AdMemberAccount_pass', 'error');
}

function mapLevelToView(level) {
    var html = '<option selected="selected" value="popularity">Mời chọn...</option>';
    for(var i=0; i<level.length; i++) {
        html += '<option value="' + level[i].maCB + '">' + level[i].tenCB + '</option>';
    }
    ISS.DOM.html('#AdMemberAccount_level', html);
}

function mapAllMemberCodeToView(member) {
    var html = '<option selected="selected" value="popularity">Mời chọn...</option>';
    for(var i=0; i<member.length; i++) {
        html += '<option value="' + member[i].maTV + '">' + member[i].maTV + '</option>';
    }
    ISS.DOM.html('#AdMemberAccount_user', html);
}

function mapAllAccountToView(member) {

    var rows = member.length;
    var columns = 1;
    var str = '';
    str += '<table class="scroll align-center">';
    str += '<thead>';
    str += '   <tr>';
    str += '       <th class="columnSTT">STT</th>';
    str += '       <th class="columnStick">Tick</th>';
    str += '       <th class="columnRule">Mã thành viên</th>';
    str += '       <th class="columnRule">Mật khẩu</th>';
    str += '       <th class="columnRule">Mã cấp bậc</th>';
    str += '   </tr>';
    str += '</thead>';
    str += '<tbody>';
    for(var i = 0; i < rows; i++){
        if (i%2==0)
        {
            str += '<tr class="column-Dark">';
        }
        else
        {
            str += '<tr class="column-Light">';
        }

        for (var j = 0; j < columns; j++){
            str += '<td class="columnSTT">' + i + '</td>';
            str += '<td class="columnStick"><input id="' + member[i].thongTinThanhVien.maTV + '" type="checkbox" value="' + member[i].thongTinThanhVien.maTV+ '"/></td>';
            str += '<td class="columnRule">' + member[i].thongTinThanhVien.maTV + '</td>';
            str += '<td class="columnRule">' + member[i].matKhau + '</td>';
            str += '<td class="columnRule">' + member[i].capBac.maCB + '</td>';
        }
        str += '</tr>';
    }
    str +=   '</tbody>';
    str += '</table>';
    ISS.DOM.html('#RegeditForm_member_All', str);
}